package com.ruoyi.gcgl.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.gcgl.mapper.GcProjectSbclhtFjMapper;
import com.ruoyi.gcgl.mapper.GcProjectSbclhtMapper;
import com.ruoyi.gcgl.domain.GcProjectSbclht;
import com.ruoyi.gcgl.domain.GcProjectSbclhtFj;
import com.ruoyi.gcgl.service.IGcProjectSbclhtService;
import com.ruoyi.common.core.text.Convert;

/**
 * 设备材料合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-29
 */
@Service
public class GcProjectSbclhtServiceImpl implements IGcProjectSbclhtService 
{
    @Autowired
    private GcProjectSbclhtMapper gcProjectSbclhtMapper;
    @Autowired
    private GcProjectSbclhtFjMapper gcProjectSbclhtFjMapper;

    /**
     * 查询设备材料合同
     * 
     * @param id 设备材料合同主键
     * @return 设备材料合同
     */
    @Override
    public GcProjectSbclht selectGcProjectSbclhtById(Long id){
    	GcProjectSbclht obj = gcProjectSbclhtMapper.selectGcProjectSbclhtById(id);
    	
    	GcProjectSbclhtFj sbclhtFj = new GcProjectSbclhtFj();
    	sbclhtFj.setDeleteFlag(1);
    	sbclhtFj.setType("1");
    	sbclhtFj.setSbclhtId(id);
    	List<GcProjectSbclhtFj> fj = gcProjectSbclhtFjMapper.selectGcProjectSbclhtFjList(sbclhtFj);
    	
    	if(fj!=null && fj.size()>0){
    		obj.setZlwj(fj.get(0).getZilwj());
    	}
    	
    	GcProjectSbclhtFj htsbclhtFj = new GcProjectSbclhtFj();
    	htsbclhtFj.setDeleteFlag(1);
    	htsbclhtFj.setType("3");
    	htsbclhtFj.setSbclhtId(id);
    	List<GcProjectSbclhtFj> fjht = gcProjectSbclhtFjMapper.selectGcProjectSbclhtFjList(htsbclhtFj);
    	
    	if(fjht!=null && fjht.size()>0){
    		obj.setHzzszlwj(fjht.get(0).getZilwj());
    	}
    	
        return obj;
    }

    /**
     * 查询设备材料合同列表
     * 
     * @param gcProjectSbclht 设备材料合同
     * @return 设备材料合同
     */
    @Override
    public List<GcProjectSbclht> selectGcProjectSbclhtList(GcProjectSbclht gcProjectSbclht)
    {
        return gcProjectSbclhtMapper.selectGcProjectSbclhtList(gcProjectSbclht);
    }

    /**
     * 新增设备材料合同
     * 
     * @param gcProjectSbclht 设备材料合同
     * @return 结果
     */
    @Override
    public int insertGcProjectSbclht(GcProjectSbclht gcProjectSbclht){
    	
    	gcProjectSbclhtMapper.insertGcProjectSbclht(gcProjectSbclht);
    	
    	GcProjectSbclhtFj gcProjectSbclhtFj = new GcProjectSbclhtFj();
    	gcProjectSbclhtFj.setDeleteFlag(1);
    	gcProjectSbclhtFj.setCreateAt(new Date());
    	gcProjectSbclhtFj.setUpdateAt(new Date());
    	gcProjectSbclhtFj.setGcProjectid(gcProjectSbclht.getGcProjectid());
    	gcProjectSbclhtFj.setOrderNo(gcProjectSbclht.getOrderNo());
    	gcProjectSbclhtFj.setSbclhtId(gcProjectSbclht.getId());
    	gcProjectSbclhtFj.setType("1");//1合同，2询价单
    	gcProjectSbclhtFj.setZilwj(gcProjectSbclht.getZlwj());
    	gcProjectSbclhtFjMapper.insertGcProjectSbclhtFj(gcProjectSbclhtFj);
    	
        return 1;
    }

    /**
     * 修改设备材料合同
     * 
     * @param gcProjectSbclht 设备材料合同
     * @return 结果
     */
    @Override
    public int updateGcProjectSbclht(GcProjectSbclht gcProjectSbclht){
    	
    	GcProjectSbclhtFj gcProjectSbclhtFj = new GcProjectSbclhtFj();
    	gcProjectSbclhtFj.setDeleteFlag(1);
    	gcProjectSbclhtFj.setGcProjectid(gcProjectSbclht.getGcProjectid());
    	gcProjectSbclhtFj.setOrderNo(gcProjectSbclht.getOrderNo());
    	gcProjectSbclhtFj.setSbclhtId(gcProjectSbclht.getId());
    	gcProjectSbclhtFj.setType("1");//1合同，2询价单
    	List<GcProjectSbclhtFj> lis = gcProjectSbclhtFjMapper.selectGcProjectSbclhtFjList(gcProjectSbclhtFj);
    	
    	if(lis!=null && lis.size()>0){
    		GcProjectSbclhtFj obj = lis.get(0);
        	obj.setZilwj(gcProjectSbclht.getZlwj());
        	gcProjectSbclhtFjMapper.updateGcProjectSbclhtFj(obj);
    	}else{
        	gcProjectSbclhtFj.setCreateAt(new Date());
        	gcProjectSbclhtFj.setUpdateAt(new Date());
        	gcProjectSbclhtFj.setZilwj(gcProjectSbclht.getZlwj());
        	gcProjectSbclhtFjMapper.insertGcProjectSbclhtFj(gcProjectSbclhtFj);
    	}
    	
        return gcProjectSbclhtMapper.updateGcProjectSbclht(gcProjectSbclht);
    }

    /**
     * 批量删除设备材料合同
     * 
     * @param ids 需要删除的设备材料合同主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectSbclhtByIds(String ids)
    {
        return gcProjectSbclhtMapper.deleteGcProjectSbclhtByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除设备材料合同信息
     * 
     * @param id 设备材料合同主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectSbclhtById(Long id)
    {
        return gcProjectSbclhtMapper.deleteGcProjectSbclhtById(id);
    }
}
